package com.twang.stock.strategy;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.Entity.ShiShiData;
import com.twang.stock.config.StockMsgSender;
import com.twang.stock.dto.EmailMessage;
import com.twang.stock.entity.CompanyStockMinutes;
import com.twang.stock.entity.KaiPanLa;
import com.twang.stock.entity.KaiPanLaBaseInfo;
import com.twang.stock.entity.User;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.UserMapper;
import com.twang.stock.service.MinutesChengJIaoeService;
import com.twang.stock.service.jinyan.CommonService;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *         List<TenXunStock> tenXunStocks = HtmlTools.httpGet(key);
 */
@Service
@Slf4j
public class OneMinutesTradeSum {

    @Autowired
    private CommonService commonService;

    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    @Resource
    private StockMsgSender stockMsgSender;

    @Resource
    private UserMapper userMapper;

    @Autowired
    private AsycTaskExecutorConfig asycTaskExecutorConfig;

    @Resource
    private Stock60MinutesUpdate stock60MinutesUpdate;
    /**
     *
     */
    public void max60DayTradeVol(ShiShiData shiShiData) {
        //List<String> dataList = HtmlTools.httpTenXun(code);
       // String result = dataList.get(0);
       // List<String> strings = JSONObject.parseArray(result, String.class);
        //当前最新
       // String[] dataNew = strings.get(strings.size() - 1).split(" ");
       // CompanyStockMinutes companyStockMinutes = 处理分时转换(dataNew, code);

        CompanyStockMinutes companyStockMinutes = new CompanyStockMinutes();
        //companyStockMinutes.setMinutes(data[0]);
        companyStockMinutes.setPrice(new BigDecimal(shiShiData.getPrice()));
        companyStockMinutes.setTradeVol(new BigDecimal(shiShiData.getSumvol()));
        companyStockMinutes.setTradeSum(new BigDecimal(shiShiData.getAmount()));
        companyStockMinutes.setCreateTime(new Date());
        companyStockMinutes.setCompanyCode(shiShiData.getCode());
        companyStockMinutes.setCompanyName(shiShiData.getGpname());
        companyStockMinutes.setZhangDie(shiShiData.getZhangdie());
        //判断是否触发60天内预警
        stock60MinutesUpdate.warnMax60Vol(companyStockMinutes);
        //1分钟涨幅预警
       // twoMinutesZhangDie(code, strings, companyStockMinutes);
    }

    /**
     * 两分钟涨跌
     * @param code
     * @param strings
     * @param companyStockMinutes
     */
    private void twoMinutesZhangDie(String code, List<String> strings, CompanyStockMinutes companyStockMinutes) {
        String[] dataLast = strings.get(strings.size() - 2).split(" ");
        CompanyStockMinutes companyStockMinutesLats = 处理分时转换(dataLast, code);

        //2分钟
        String[] 两分钟 = strings.get(strings.size() - 3).split(" ");
        CompanyStockMinutes 两分钟MinutesLats = 处理分时转换(两分钟, code);
        BigDecimal 两分钟震幅 = companyStockMinutes.getZhangDie().subtract(两分钟MinutesLats.getZhangDie());
        BigDecimal 两分钟成交额 = companyStockMinutes.getTradeSum().subtract(两分钟MinutesLats.getTradeSum());
        Boolean 两分钟震幅Flag = new BigDecimal(2).compareTo(两分钟震幅) < 0 && new BigDecimal(10000000).compareTo(两分钟成交额) < 0;

        //3分钟
        String[] 三分钟 = strings.get(strings.size() - 4).split(" ");
        CompanyStockMinutes 三分钟MinutesLats = 处理分时转换(三分钟, code);
        BigDecimal 三分钟震幅 = companyStockMinutes.getZhangDie().subtract(三分钟MinutesLats.getZhangDie());
        BigDecimal 三分钟成交额 = companyStockMinutes.getTradeSum().subtract(三分钟MinutesLats.getTradeSum());
        Boolean 三分钟震幅Flag = new BigDecimal(2).compareTo(三分钟震幅) < 0 && new BigDecimal(10000000).compareTo(三分钟成交额) < 0;


        BigDecimal 一分钟震幅 = companyStockMinutes.getZhangDie().subtract(companyStockMinutesLats.getZhangDie());
        BigDecimal 成交额 = companyStockMinutes.getTradeSum().subtract(companyStockMinutesLats.getTradeSum());
        Boolean 一分钟震幅Flag = new BigDecimal(2).compareTo(一分钟震幅) < 0 && new BigDecimal(10000000).compareTo(成交额) < 0;
        if (一分钟震幅Flag || 三分钟震幅Flag || 两分钟震幅Flag) {
            BigDecimal tradeMoney = BigDecimal.ZERO;
            BigDecimal 震幅 = BigDecimal.ZERO;
            if (一分钟震幅Flag) {
                tradeMoney = 成交额.divide(new BigDecimal(10000000)).setScale(2, BigDecimal.ROUND_HALF_UP);
                震幅 = 一分钟震幅;
            }
            if (两分钟震幅Flag) {
                tradeMoney = 两分钟成交额.divide(new BigDecimal(15000000)).setScale(2, BigDecimal.ROUND_HALF_UP);
                震幅 = 两分钟震幅;
            }
            if (三分钟震幅Flag) {
                tradeMoney = 三分钟成交额.divide(new BigDecimal(20000000)).setScale(2, BigDecimal.ROUND_HALF_UP);
                震幅 = 三分钟震幅;
            }
            log.info(">>>>>>>>>>> 触发预警： 震幅：{} 成交额：{}万", 震幅, tradeMoney);
            String reason = "";
            try {
                KaiPanLa kaiPanLa = MinutesChengJIaoeService.getGetDayZhangTing(code.replace("sh", "").replace("sz", ""));
                log.info("读取第三方：code={}，result={}", code, JSONObject.toJSONString(kaiPanLa));
                List<KaiPanLaBaseInfo> list = kaiPanLa.getList();
                if (CollectionUtils.isNotEmpty(list)) {
                    reason = list.get(0).getReason();
                }
            } catch (Exception e) {
            }
            String messages = new StringBuffer().append("触发分时涨幅超过2%!!!涨幅：").append(震幅.setScale(2, BigDecimal.ROUND_HALF_UP)).append("成交额：")
                    .append(tradeMoney).append("千万</br>").append("【涨停原因：").append(StringUtils.isEmpty(reason) ? "未查询到" : reason).append("】").toString();
            EmailMessage emailMessage = new EmailMessage();
            emailMessage.setCode(companyStockMinutesLats.getCompanyCode());
            emailMessage.setName(companyStockMinutesLats.getCompanyName());
            emailMessage.setMessage(messages);
            emailMessage.setType("3");
            List<String> ids = Arrays.asList(commonService.getValue("WARN_CJE_ID").toString().split(","));
            for (String id : ids) {
                User user = userMapper.selectByPrimaryKey(Long.valueOf(id));
                emailMessage.setEmail(user.getEmail());
                stockMsgSender.sendEmailMessage(JSONObject.toJSONString(emailMessage));
            }
        }
    }

    /**
     *
     * @param data
     * @return
     */
    private  CompanyStockMinutes 处理分时转换(String[] data,String code){
        BigDecimal yesterDayPrice=BigDecimal.ZERO;
        CompanyStockMinutes companyStockMinutes = new CompanyStockMinutes();
        companyStockMinutes.setMinutes(data[0]);
        companyStockMinutes.setPrice(new BigDecimal(data[1]));
        companyStockMinutes.setTradeVol(new BigDecimal(data[2]));
        companyStockMinutes.setTradeSum(new BigDecimal(data[3]));
        companyStockMinutes.setCreateTime(new Date());
        companyStockMinutes.setCompanyCode(code);
        List<String> dates = companyStockMinutesMapper.queryDates();
        /** 昨天 */
        String date = dates.get(dates.size() - 2);
        List<CompanyStockMinutes> companyStockMinutesList = companyStockMinutesMapper.queryYesterDayInfo(companyStockMinutes.getCompanyCode(), date);
        if (CollectionUtils.isNotEmpty(companyStockMinutesList)) {
            yesterDayPrice = companyStockMinutesList.get(0).getPrice();
            companyStockMinutes.setCompanyName(companyStockMinutesList.get(0).getCompanyName());
            companyStockMinutes.setCompanyCode(companyStockMinutesList.get(0).getCompanyCode());
        }
        companyStockMinutes.setZhangDie((companyStockMinutes.getPrice().subtract(yesterDayPrice).divide(yesterDayPrice, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))));

        return companyStockMinutes;
    }

    public static void main(String[] args) {
        //分钟拉升成交额("sz002261");
    }
}
